<?php

namespace app\bews\model;
use think\app;
use think\exception\HttpResponseException;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\View;

class Base
{
    public $aUser = [];
    public function __construct(){  //构造方法默认调用
//        $admin_id = Cookie::get("admin_id");
        $admin_id = 1;
        if(empty($admin_id)){
            header('location:./bews/Login/index');
        }
        $aUser = Db::table("bew_admin_user")->where("uid",$admin_id)->find();
        $this->aUser = $aUser;
        if(empty($aUser)){
            Cookie::delete("admin_id");
            header('location:./bews/Login/index');
        }
        if($aUser['status'] != 1){
            Cookie::delete("admin_id");
            $this->error("管理员账号已被禁用");
            header('location:./bews/Login/index');
        }
        /*$controller = request()->controller();
        $action = request()->action();

        $key = "../".$controller.$action;
        $url = Db::table("bew_admin_sys-menu")->where('src',$key)->find();
        if($url == null){
            $this->error("您访问的页面不存在");
            exit();
        }*/
//        $group = Db::table("bew_admin_user_group")->where('group_id',$aUser['group_id'])->find();
        View::assign([
            'user'=>$aUser
        ]);
    }

    protected function error(string $string)
    {
        $arr = [
            'code'=>0,
            'msg'=>$string
        ];
        $response = view(Config::get("app.exception_error_tmpl"),$arr);
        throw new HttpResponseException($response);
    }
}